
/**
 * @param {Array} params
 */
export function ArrangeParams(params) {
  params = params || {}
  let newParams = {}
  Object.keys(params).forEach((item) => {
    if ((params[item] || params[item] === 0) && (params[item] + '').length) {
      newParams[item] = params[item]
    }
  })
  return newParams
}


/**
 * @param {Array} jsonData
 * @param {String} id
 * @param {String} pid
 */
export function jsonToTree(jsonData, id, pid) {
	let result = [],
	temp = {};
	for (let i = 0; i < jsonData.length; i++) {
		temp[jsonData[i][id]] = jsonData[i]; // 以id作为索引存储元素，可以无需遍历直接定位元素
	}
	for (let j = 0; j < jsonData.length; j++) {
		let currentElement = jsonData[j];
		let tempCurrentElementParent = temp[currentElement[pid]]; // 临时变量里面的当前元素的父元素
		if (tempCurrentElementParent) {
			// 如果存在父元素
			if (!tempCurrentElementParent["children"]) {
			// 如果父元素没有chindren键
				tempCurrentElementParent["children"] = []; // 设上父元素的children键
			}
			tempCurrentElementParent["children"].push(currentElement); // 给父元素加上当前元素作为子元素
		} else {
			// 不存在父元素，意味着当前元素是一级元素
			result.push(currentElement);
		}
	}
	return result;
}


/**
 * @param {String} date
 */
export function  getLastSevenDays(date){
		var date = date || new Date(),timestamp, newDate;
		if(!(date instanceof Date)){
			date = new Date(date.replace(/-/g, '/'));
		}
		timestamp = date.getTime();
		newDate = new Date(timestamp - 7 * 24 * 3600 * 1000);
		var month = newDate.getMonth() + 1;
		month = month.toString().length == 1 ? '0' + month : month; 
		var day = newDate.getDate().toString().length == 1 ? '0' + newDate.getDate() :newDate.getDate();
	    let date1 = [newDate.getFullYear(), month, day].join('-');
	   return date1 + " 00:00:00"
}

/**
 * 获取前面第几天或后面第几天的时间，前面第5天输入-5，后面第5天输入5
 * @param {Number} Days
 */
export function getDaysDate(Days) {
  console.log(Days,'Days');
  let Num = Days || 0;
  let date = new Date();
  let Time = (date.getTime() + 3600 * 1000 *24 * Num);
  return getDateFormat(Time,'');
}


/**
 * 判断是否两位数，否则补在前面补0
 * @param {String} n 
 */
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * @param {String} datestr
 * @param {String} pattern
 */
export function getDateFormat(datestr,pattern = ""){
    let dateStr = datestr || new Date();
    var date = new Date(dateStr)
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    if (pattern.toLowerCase() === 'yyyy-mm-dd') { 
        return [year, month, day].map(formatNumber).join('-')
    } else if (pattern.toLowerCase() === 'hh:mm:ss'){
        return [hour, minute, second].map(formatNumber).join(':')
    } else{
        return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    } 
}
